package com.bnlr.lrparking.adapter;

import android.animation.Keyframe;
import android.animation.ObjectAnimator;
import android.animation.PropertyValuesHolder;
import android.content.Intent;
import android.support.annotation.Nullable;
import android.view.View;
import android.widget.CheckedTextView;
import android.widget.ImageView;
import android.widget.RelativeLayout;

import com.bumptech.glide.Glide;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.bnlr.lrparking.app.Application;
import com.bnlr.lrparking.R;
import com.bnlr.lrparking.appui.stall.InvestActivity;
import com.bnlr.lrparking.app.AppConfig;
import com.bnlr.lrparking.model.StallData;
import com.bnlr.lrparking.model.StallM;
import com.bnlr.lrparking.constant.LrConstant;
import com.bnlr.lrparking.constant.Urls;
import com.bnlr.lrparking.view.CarCommon;
import com.bnlr.nohttp.CallServer;
import com.bnlr.nohttp.CustomHttpListener;
import com.yolanda.nohttp.NoHttp;
import com.yolanda.nohttp.rest.Request;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.List;

/**
 * Created by tian on 2017/9/7.
 */

public class SearchStallAdapter extends BaseQuickAdapter<StallData,BaseViewHolder> {
    public Request<String> mRequest;
    public SearchStallAdapter(@Nullable List<StallData> data) {
        super(R.layout.item_stall,data);
    }

    @Override
    protected void convert(final BaseViewHolder holder, final StallData item) {
        holder.setIsRecyclable(false);
        ImageView iv = holder.getView(R.id.img_stall);
        Glide.with(mContext)
                .load(item.getParkPhoto())
                .placeholder(R.mipmap.tab2_defaultgraph)
                .error(R.mipmap.tab2_defaultgraph)
                .into(iv);
        holder.setText(R.id.tv_stall_type,item.getOwnerType());
        holder.setText(R.id.tv_stall_name,item.getParkName());
        holder.setText(R.id.tv_stall_number,"车位编号："+item.getNumber());
        holder.setText(R.id.tv_stall_time,"投资时间："+item.getBeginTime()+"-"+item.getEndTime());
        holder.setText(R.id.tv_stall_num,""+item.getSellingPrice());
        holder.setText(R.id.tv_stall_earnin_num,""+item.getIncomeRockon());
        final CheckedTextView checkText = holder.getView(R.id.checkBox_college);
        if (item.getIsFavorite().equals("0")){
            checkText.setChecked(false);
        }else if (item.getIsFavorite().equals("1")){
            checkText.setChecked(true);
        }
        RelativeLayout rely_goinvest = holder.getView(R.id.rely_goinvest);
        RelativeLayout stall_phone = holder.getView(R.id.stall_phone);
        final RelativeLayout rely_college = holder.getView(R.id.rely_college);
        rely_goinvest.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent=new Intent(mContext,InvestActivity.class);
                intent.putExtra("monthCount",item.getMonthCount());
                intent.putExtra("number",item.getNumber());//
                intent.putExtra("id",item.getId());
                intent.putExtra("sellingprice",item.getSellingPrice());
                mContext.startActivity(intent);
                AppConfig.getInstance().putString("isList", "0");//是从哪个列表去支付界面的
            }
        });
        stall_phone.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                CarCommon carCommon = new CarCommon();
                carCommon.phoneDialog(mContext);
            }
        });
        rely_college.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                startShakeByPropertyAnim(rely_college, 0.9f, 1.2f, 11f, 500);
                if (item.getIsFavorite().equals("0")){
                    setCollege(item.getIsFavorite(),"FavoriteSpace", item.getId(),checkText,item,holder.getAdapterPosition());
                }else if (item.getIsFavorite().equals("1")){
                    setCollege(item.getIsFavorite(),"CancelFavoriteSpace", item.getId(),checkText,item,holder.getAdapterPosition());
                }
            }
        });
    }

    //收藏的接口
    private void setCollege(final String is, final String FavoriteSpace, String id, final CheckedTextView checkedTextView, final StallData stallData, final int adapterPosition){
        try {
            mRequest = NoHttp.createStringRequest(Urls.SPACE_IP, LrConstant.POST);
            /**
             * IsFavorite: 0是未收藏  1是已收藏
             * action: FavoriteSpace是从藏接口  CancelFavoriteSpace是取消收藏接口
             */
            mRequest.add("action", FavoriteSpace);
            mRequest.add("phonePara", Application.DEVICE_ID);
            mRequest.add("uid", AppConfig.getInstance().getString("uid", ""));
            mRequest.add("id", id);

            CallServer.getRequestInstance().add(mContext, mRequest,
                    new CustomHttpListener<StallM>(mContext, true, StallM.class) {
                        @Override
                        public void doWork(StallM data, String code) {
                            if ("1".equals(code)) {
                                if (is.equals("0")) {
                                    checkedTextView.setChecked(true);
                                    stallData.setIsFavorite("1");
                                } else if (is.equals("1")) {
                                    checkedTextView.setChecked(false);
                                    stallData.setIsFavorite("0");
                                    mData.remove(adapterPosition);
                                }
                                notifyDataSetChanged();
                            }
                        }
                        @Override
                        public void onFinally(JSONObject obj, String code, boolean isSucceed) {
                            try {
                                super.onFinally(obj, code, isSucceed);
                            } catch (JSONException e) {
                                e.printStackTrace();
                            }
                        }
                    }, true);
        } catch (Exception e) {

        }
    }

    private void startShakeByPropertyAnim(View view, float scaleSmall, float scaleLarge, float shakeDegrees, long duration) {
        if (view == null) {
            return;
        }
        //TODO 验证参数的有效性

        //先变小后变大
        PropertyValuesHolder scaleXValuesHolder = PropertyValuesHolder.ofKeyframe(View.SCALE_X,
                Keyframe.ofFloat(0f, 1.0f),
                Keyframe.ofFloat(0.25f, scaleSmall),
                Keyframe.ofFloat(0.5f, scaleLarge),
                Keyframe.ofFloat(0.75f, scaleLarge),
                Keyframe.ofFloat(1.0f, 1.0f)
        );
        PropertyValuesHolder scaleYValuesHolder = PropertyValuesHolder.ofKeyframe(View.SCALE_Y,
                Keyframe.ofFloat(0f, 1.0f),
                Keyframe.ofFloat(0.25f, scaleSmall),
                Keyframe.ofFloat(0.5f, scaleLarge),
                Keyframe.ofFloat(0.75f, scaleLarge),
                Keyframe.ofFloat(1.0f, 1.0f)
        );

        ObjectAnimator objectAnimator = ObjectAnimator.ofPropertyValuesHolder(view, scaleXValuesHolder, scaleYValuesHolder, scaleYValuesHolder);
        objectAnimator.setDuration(duration);
        objectAnimator.start();
    }
}
